# ======================================================================
#
# Copyright (C) ADN
# ADN::SERVER::WebHTML
# Description:
#  - Cree les pages HTML
#  - Create the HTML page
#
# ======================================================================

package ADN::SERVER::WebHTML;

use strict;

sub new {
	  my $self = shift;
	  my $class = ref($self) || $self;
	  return $self if ref $self;
	  $self = bless {} => $class;
    my $var = ADN::CONF::Read->new();
    %{$self->{var}} = $var->web();
    %{$self->{server}} = $var->server();
		undef $var;
		return $self->_init(@_);
}

sub _init {
		my $self = shift;
    %{$self->{param}} = @_;
    if ( $self->{param}{Opt} == 1 ) {
			$self->my_logHtml();
    }
    elsif ( $self->{param}{Opt} == 2 ) {
			$self->my_htmlLog();
    }
    elsif ( $self->{param}{Opt} == 3 ) {
			$self->my_procHtml();
    }
    elsif ( $self->{param}{Opt} == 4 ) {
			$self->my_errorHtml( );
    }
    return $self->{result};
}

# #######################
# CREATE THE LOG FILE
# #######################
sub my_htmlLog {
		my $self = shift;
    $self->{result} = "\r\n";
    foreach ( @{$self->{param}{Log}} ) {
			$self->{result} .= $_ ;
    }
    return;
}

# #######################
# CREATE THE HTML LOG
# #######################
sub my_logHtml {
		my $self = shift;
    $self->{read_Log} = '';
    foreach ( @{$self->{param}{Log}} ) {
			$self->{read_Log} .= $_ . '<br />';
    }
    return $self->my_html('Log File');
}

# #######################
# CREATE THE HTML PROCESS
# #######################
sub my_procHtml {
		my $self = shift;
    my ( $nbr, $i );
    $nbr = scalar @{$self->{param}{Log}};
    $self->{read_Log} = '';
    for ( $i = 0; $i < $nbr; $i++ ) {
			if (defined @{$self->{param}{Log}}[$i]) {
	    	$self->{read_Log} .= @{$self->{param}{Log}}[$i] . '<br />';
			}
    }
    return $self->my_html('Process');
}

# #######################
# PROTECTION SCRIPT
# #######################
sub protect {
		my $self = shift;
		$self->{script} = '';
    if ( $self->{var}{option} == 1 ) {
			$self->{script} = <<"JAVA";
	<script type="text/javascript">
	    <!--hide
	    function createCookie(name,value,days) {
		if (days) {
		    var date = new Date();
		    date.setTime(date.getTime()+(days*24*60*60*1000));
		    var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	}
	function readCookie(name) {
	    var nameEQ = name + "=";
	    var ca = document.cookie.split(';');
	    for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	    }
	    return null;
	}
	if ( readCookie("ADN-SONDE") == null ) {
			var pass;
			var p = "$self->{var}{password}";
	    pass = prompt('Password','');
	    if ( !( pass == p ) ) {
				window.location="http://$self->{server}{address}:$self->{server}{port}/errs";
	    }
	    else {
				createCookie("ADN-SONDE","auth","");
	    }
	}
	//-->
	    </script>
	    <noscript>
	      <meta http-equiv="refresh" content="0" url="http://$self->{server}{address}:$self->{server}{port}/errs" />
	    </noscript>
JAVA
    }
		return;
}

# #######################
# CREATE THE HTML MENU
# #######################
sub menu {
	my $self = shift;
  $self->{menu} = '';
  if ( $self->{var}{log} == 1 ) {
    $self->{menu} .= ' [ <a class="menu_log" href="slog">LOG</a> ]';
  }
  if ( $self->{var}{process} == 1 ) {
    $self->{menu} .= ' [ <a class="menu_process" href="proc">PROCESS</a> ]';
  }
  if ( $self->{var}{restart} == 1 ) {
    $self->{menu} .= ' [ <a class="menu_restart" href="rest">RESTART</a> ]';
  }
  return;
}

# #######################
# CREATE THE ERROR HTML
# #######################
sub my_errorHtml {
		my $self = shift;
    $self->{result} = "\r\n";
    $self->{result} .= <<"HTML";
	<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
	<html>
	<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>ADN Error</title>
	<style type="text/css">
	body {
	    font-family: Verdana,Arial,sans-serif;
	    font-size: 0.6em;
	  margin: 0;
    }
    p {
	line-height: 17px;
      margin: 11px 0 10px;
      padding: 0;
    }
    #wrap {
    margin-left: auto;
    margin-right: auto;
  width: 730px;
}
#content {
margin-top: 30px;
 width: 100%;
      }
      #top h2 {
      margin: 0;
      color: white;
      font-family: Verdana,Arial,sans-serif;
      margin-left: 15%;
      padding: 10px 0;
      }
      #clear {
      clear: both;
      display: block;
      height: 1px;
      overflow: hidden;
      width: 100%;
      }   
      #footer {
      border-top: 1px dotted gray;
      margin: 40px auto 0;
      padding: 20px 0;
      text-align: center;
      width: 70%;
      }
      #footer p {
      margin: 0;
      padding: 0;
      }
      #top {
      background: #5F564D none repeat scroll 0 0;
      border: 1px solid #E1E1E1;
      padding: 10px 10px 15px;
      width: 100%;
      }
    </style>
  </head>
  <body>
    <div id="top">
      <h2>Adn: Error</h2>
   </div>
    <div id="wrap">
      <div id="content">
	<div id="left">
          <p style="color : red; font-weight : bold;">
	    Wrong Password | Mauvais password
	    <br />
	    Javascript not enable | Javascript n\'est pas active 
	  </p>
	</div>
	<div id="clear"></div>
      </div>
      <div id="footer">
        <p>Copyright &copy; 2009 ADN Logiciel </p>
      </div>
    </div>
  </body>
</html>
HTML
	return;
}

# #######################
# CREATE THE HTML
# #######################
sub my_html {
		my $self = shift;
    $self->protect();
    $self->menu();
    $self->{result} = "\r\n";
    $self->{result} .= <<HTML;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>ADN $_[0]</title>
    $self->{script}
    <style type="text/css">
      body {
      font-family: Verdana,Arial,sans-serif;
      font-size: 0.6em;
      margin: 0;
      }
      p {
      line-height: 17px;
      margin: 11px 0 10px;
      padding: 0;
      }
      .menu_main {
      text-decoration: none;
      color: #81C11A;
      }
      .menu_main:hover {
      text-decoration: none;
      color: #25A7D6;
      }
      .menu_log {
      text-decoration: none;
      color: #FF8311;
      }
      .menu_log:hover {
      text-decoration: none;
      color: #8E8275;
      }
      .menu_process {
      text-decoration: none;
      color: #25A7D6;
      }
      .menu_process:hover {
      text-decoration: none;
      color: #81C11A;
      }
      .menu_restart {
      text-decoration: none;
      color: #8E8275;
      }
      .menu_restart:hover {
      text-decoration: none;
      color: #FF8311;
      }
      #wrap {
      margin-left: auto;
      margin-right: auto;
      width: 730px;
      }
      #content {
      margin-top: 30px;
      width: 100%;
      }
      #top h2 {
      margin: 0;
      color: white;
      font-family: Verdana,Arial,sans-serif;
      margin-left: 15%;
      padding: 10px 0;
      }
      #clear {
      clear: both;
      display: block;
      height: 1px;
      overflow: hidden;
      width: 100%;
      }   
      #footer {
      border-top: 1px dotted gray;
      margin: 40px auto 0;
      padding: 20px 0;
      text-align: center;
      width: 70%;
      }
      #footer p {
      margin: 0;
      padding: 0;
      }
      #top {
      background: #5F564D none repeat scroll 0 0;
      border: 1px solid #E1E1E1;
      padding: 10px 10px 15px;
      width: 100%;
      }
    </style>
  </head>
  <body>
    <div id="top">
      <h2>Adn: $_[0]</h2>
      <span style="margin-left: 15%;">[</span> <a class="menu_main" href="main">MAIN</a> ]
      $self->{menu}
    </div>
    <div id="wrap">
      <div id="content">
	<div id="left">
          <p>
	    $self->{read_Log}
	  </p>
	</div>
	<div id="clear"></div>
      </div>
      <div id="footer">
        <p>Copyright &copy; 2009 ADN Logiciel </p>
      </div>
    </div>
  </body>
</html>
HTML
	return;
}

# #######################
# DESTROY OBJECT
# #######################
sub DESTROY {
    my $self = shift;
    return;
}
1;
__END__
